package cn.player.adapter;

import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import java.util.List;

import cn.player.R;
import cn.player.entity.TestBean;
import cntv.player.core.util.LogUtil;

/**
 * Created by zengliang on 16/11/28.
 */

public class RecycleViewItemAdapter extends RecyclerView.Adapter<ViewHolder> {

    public static final String TAG = "zl";
    public static final int TYPE_ONE = 1;
    public static final int TYPE_TWO = 2;
    private List<TestBean> mData;

    public void setupRecycleViewData(List<TestBean> data) {
        this.mData = data;
    }


    public static class ItemTypeOneHolder extends RecyclerView.ViewHolder {

        public LinearLayout llRoot;
        public TextView tvName;
        public TextView tvDescription;

        public ItemTypeOneHolder(View view) {
            super(view);
            llRoot = (LinearLayout) view.findViewById(R.id.ll_item_root);
            tvName = (TextView) view.findViewById(R.id.tv_item_name);
            tvDescription = (TextView) view.findViewById(R.id.tv_item_description);

        }
    }

    public static class ItemTypeTwoHolder extends RecyclerView.ViewHolder {

        public RelativeLayout rlRoot;
        public TextView tvName;
        public TextView tvDescription;

        public ItemTypeTwoHolder(View view) {
            super(view);
            rlRoot = (RelativeLayout) view.findViewById(R.id.rl_item_root);
            tvName = (TextView) view.findViewById(R.id.tv_item_name);
            tvDescription = (TextView) view.findViewById(R.id.tv_item_description);
        }
    }

    public void notifyRemoved(int position) {
        mData.remove(position);
        notifyItemRemoved(position);
    }

    public void notifyInserted(int position, TestBean testBean) {
        mData.add(position, testBean);
        notifyItemInserted(position);
    }

    public void notifyChanged(int position,TestBean testBean) {
        mData.set(position,testBean);
        notifyItemChanged(position);
    }

    public void notifyChanged(int positionStart, int itemCount) {
        notifyItemRangeChanged(positionStart, itemCount);
    }


    @Override
    public int getItemCount() {
        return mData == null ? 0 : mData.size();
    }

    @Override
    public int getItemViewType(int position) {
        return position % 2 == 0 ? TYPE_ONE : TYPE_TWO;
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        LogUtil.i(TAG, "onCreateViewHolder(),viewType = " + viewType);
        switch (viewType) {
            case TYPE_ONE:
                return new ItemTypeOneHolder(View.inflate(parent.getContext(), R.layout.recycleview_item_type_one, null));
            case TYPE_TWO:
                return new ItemTypeOneHolder(View.inflate(parent.getContext(), R.layout.recycleview_item_type_two, null));
        }
        return null;
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        LogUtil.i(TAG, "onBindViewHolder(),position = " + position);
        if (holder instanceof ItemTypeOneHolder) {
            ItemTypeOneHolder itemTypeOneHolder = ((ItemTypeOneHolder) holder);
            itemTypeOneHolder.tvName.setText(mData.get(position).getName());
            itemTypeOneHolder.tvDescription.setText(mData.get(position).getDescription());
        } else if (holder instanceof ItemTypeTwoHolder) {
            ItemTypeTwoHolder itemTypeTwoHolder = ((ItemTypeTwoHolder) holder);
            itemTypeTwoHolder.tvName.setText(mData.get(position).getName());
            itemTypeTwoHolder.tvDescription.setText(mData.get(position).getDescription());
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position, List<Object> payloads) {
        super.onBindViewHolder(holder, position, payloads);
        LogUtil.i(TAG, "onBindViewHolder(),position = " + position + ",payloads = " + payloads);

    }

    @Override
    public void setHasStableIds(boolean hasStableIds) {
        super.setHasStableIds(hasStableIds);
        LogUtil.i(TAG, "setHasStableIds(),hasStableIds = " + hasStableIds);
    }

    @Override
    public long getItemId(int position) {
        LogUtil.i(TAG, "getItemId(),position = " + position);
        return super.getItemId(position);
    }

    @Override
    public void onViewRecycled(ViewHolder holder) {
        super.onViewRecycled(holder);
        LogUtil.i(TAG, "onViewRecycled(),holder = " + holder);
    }

    @Override
    public boolean onFailedToRecycleView(ViewHolder holder) {
        LogUtil.i(TAG, "onFailedToRecycleView(),holder = " + holder);
        return super.onFailedToRecycleView(holder);
    }

    @Override
    public void onViewAttachedToWindow(ViewHolder holder) {
        super.onViewAttachedToWindow(holder);
        LogUtil.i(TAG, "onViewAttachedToWindow(),holder = " + holder);
    }

    @Override
    public void onViewDetachedFromWindow(ViewHolder holder) {
        super.onViewDetachedFromWindow(holder);
        LogUtil.i(TAG, "onViewDetachedFromWindow(),holder = " + holder);
    }

    @Override
    public void registerAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        super.registerAdapterDataObserver(observer);
        LogUtil.i(TAG, "registerAdapterDataObserver(),observer = " + observer);
    }

    @Override
    public void unregisterAdapterDataObserver(RecyclerView.AdapterDataObserver observer) {
        super.unregisterAdapterDataObserver(observer);
        LogUtil.i(TAG, "unregisterAdapterDataObserver(),observer = " + observer);
    }

    @Override
    public void onAttachedToRecyclerView(RecyclerView recyclerView) {
        super.onAttachedToRecyclerView(recyclerView);
        LogUtil.i(TAG, "onAttachedToRecyclerView(),recyclerView = " + recyclerView);
    }

    @Override
    public void onDetachedFromRecyclerView(RecyclerView recyclerView) {
        super.onDetachedFromRecyclerView(recyclerView);
        LogUtil.i(TAG, "onDetachedFromRecyclerView(),recyclerView = " + recyclerView);
    }
}
